﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;

namespace QuanLyKho.DKChung
{
    public partial class DKChung_DkNhanVienPopup : Form
    {
        public static string MaID = "0";
        public bool update = false;

        public DKChung_DkNhanVienPopup()
        {
            InitializeComponent();
            LoadData();
        }

        public void LoadData()
        {
            if (MaID != "0")
            {
                var data = DataAll.SelectAll("DkChung_NhanVien", "*", " WHERE MaIDNhanVien=" + MaID);
                txtMaNhanVien.Text = data.Rows[0]["MaNhanVien"].ToString();
                txtTenNhanVien.Text = data.Rows[0]["TenNhanVien"].ToString();
                txtSoDienThoai.Text = data.Rows[0]["SoDienThoai"].ToString();
                txtGhiChu.Text = data.Rows[0]["GhiChu"].ToString();
                txtTenDangNhap.Text = data.Rows[0]["Username"].ToString();
                txtMatKhau.Text = data.Rows[0]["Password"].ToString();
            }
        }

        public void ResetControl()
        {
            txtMaNhanVien.Text = string.Empty;
            txtTenNhanVien.Text = string.Empty;
            txtSoDienThoai.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            txtTenDangNhap.Text = string.Empty;
            txtMatKhau.Text = string.Empty;
        }

        private void bntLuuLai_Click(object sender, EventArgs e)
        {
            if (update)
            {
                string message = DataAll.Update("DkChung_NhanVien", "MaNhanVien=N'" + txtMaNhanVien.Text + "', TenNhanVien=N'" + txtTenNhanVien.Text
                    + "', SoDienThoai='" + txtSoDienThoai.Text + "',GhiChu=N'" + txtGhiChu.Text + "',Username=N'" + txtTenDangNhap.Text
                    + "',Password=N'" + txtMatKhau.Text + "' WHERE MaIDNhanVien=" + MaID);
                MessageBox.Show(message.Equals("success") ? "Nhân viên thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                Close();
            }
            else
            {
                string message = DataAll.InsertAll("DkChung_NhanVien", txtMaNhanVien.Text + "," + txtTenNhanVien.Text + "," + txtSoDienThoai.Text
                + "," + txtGhiChu.Text + "," + txtTenDangNhap.Text + "," + txtMatKhau.Text);
                MessageBox.Show(message.Equals("success") ? "Thêm nhân viên thành công" : message, "Message",
                                MessageBoxButtons.OK,
                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                if (message.Equals("success"))
                {
                    ResetControl();
                }
            }
        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cbPass_CheckedChanged(object sender, EventArgs e)
        {
            txtMatKhau.PasswordChar = cbPass.Checked ? '*' : '\0';
        }
    }
}
